package org.jeecg.modules.qyxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import org.hibernate.validator.constraints.NotBlank;
import org.jeecg.modules.qyxx.entity.BgFpHzNdWdxSxygysjxs;
import org.jeecg.modules.qyxx.entity.BgHzNdCwbb;
import org.jeecg.modules.qyxx.entity.BgHzNdCwbbHb;
import org.jeecg.modules.qyxx.entity.req.BgNsrjbxxReq;
import org.jeecg.modules.qyxx.entity.resp.QyLrFxVo;
import org.jeecg.modules.qyxx.entity.resp.QyYsAndLrFxVo;
import org.jeecg.modules.qyxx.mapper.BgHzNdCwbbHbMapper;
import org.jeecg.modules.qyxx.service.IBgHzNdCwbbHbService;
import org.jeecg.modules.qyxx.service.IBgHzNdCwbbService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 财务指标分析汇总数据环比
 * @Author: zhujianjun
 * @Date: 2022-05-10
 * @Version: V1.0
 */
@Service
public class BgHzNdCwbbHbServiceImpl extends MppServiceImpl<BgHzNdCwbbHbMapper, BgHzNdCwbbHb> implements IBgHzNdCwbbHbService {
    @Autowired
    private IBgHzNdCwbbService bgHzNdCwbbService;

    @Override
    public List<BgHzNdCwbbHb> getGjdYysrMx(BgNsrjbxxReq req) {
        DateUtils utils = DateUtils.getInstance();
        // List<String> tjnds = utils.nearlyThreeYears(req.getKssj(), req.getJssj());
        //List<BgHzNdCwbbHb> list = baseMapper.getGjdYysrMx(req.getDjxh(), String.join(",", tjnds));
         String year = utils.getYearToStr(req.getJssj());
        LambdaQueryWrapper<BgHzNdCwbbHb> wrapper = Wrappers.lambdaQuery(BgHzNdCwbbHb.class);
        wrapper.eq(BgHzNdCwbbHb::getDjxh, req.getDjxh()).eq(BgHzNdCwbbHb::getTjnd, year).orderByAsc(BgHzNdCwbbHb::getJdxh);
        List<BgHzNdCwbbHb> list = super.list(wrapper);
        for (BgHzNdCwbbHb bgHzNdCwbbHb : list) {
            if (bgHzNdCwbbHb.getYysr() != null) {
                bgHzNdCwbbHb.setYysr(bgHzNdCwbbHb.getYysr().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            }
        }
        return list;
    }

    @Override
    public List<QyYsAndLrFxVo> qyYsAndLrFx(BgNsrjbxxReq req) {
        String djxh = req.getDjxh();
        List<String> tjnds = DateUtils.getInstance().nearlyThreeYears(req.getKssj(), req.getJssj());
        List<QyYsAndLrFxVo> arrayList = new ArrayList<>();
        for (String tjnd : tjnds) {
            List<BgHzNdCwbbHb> list = baseMapper.getByDjxhAndTjnd(djxh, tjnd);
            QyYsAndLrFxVo vo = new QyYsAndLrFxVo();
            vo.setDjxh(djxh);
            vo.setTjnd(tjnd);
            BigDecimal yycbCount = BigDecimal.ZERO;
            BigDecimal yysrCount = BigDecimal.ZERO;
            BigDecimal mlrCount = BigDecimal.ZERO;
            BigDecimal jlrCount = BigDecimal.ZERO;
            for (BgHzNdCwbbHb cwbbHb : list) {
                BigDecimal yycb = cwbbHb.getYycb();
                BigDecimal yysr = cwbbHb.getYysr();
                BigDecimal mlr = cwbbHb.getMlr();
                BigDecimal jlr = cwbbHb.getJlr();
                if (yycb != null) {
                    yycb = yycb.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                    yycbCount = yycbCount.add(yycb);
                }
                if (yysr != null) {
                    yysr = yysr.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                    yysrCount = yysrCount.add(yysr);
                }
                if (mlr != null) {
                    mlr = mlr.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                    mlrCount = mlrCount.add(mlr);
                }
                if (jlr != null) {
                    jlr = jlr.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                    jlrCount = jlrCount.add(jlr);
                }
                String jdxh = cwbbHb.getJdxh();
                switch (jdxh) {
                    case "1":
                        if (yycb != null) {
                            vo.setOneJdyycb(yycb);
                        }
                        if (yysr != null) {
                            vo.setOneJdyysr(yysr);
                        }
                        if (mlr != null) {
                            vo.setOneJdmlr(mlr);
                        }
                        if (jlr != null) {
                            vo.setOneJdjlr(jlr);
                        }
                        break;
                    case "2":
                        if (yycb != null) {
                            vo.setTwoJdyycb(yycb);
                        }
                        if (yysr != null) {
                            vo.setTwoJdyysr(yysr);
                        }
                        if (mlr != null) {
                            vo.setTwoJdmlr(mlr);
                        }
                        if (jlr != null) {
                            vo.setTwoJdjlr(jlr);
                        }
                        break;
                    case "3":
                        if (yycb != null) {
                            vo.setThreeJdyycb(yycb);
                        }
                        if (yysr != null) {
                            vo.setThreeJdyysr(yysr);
                        }
                        if (mlr != null) {
                            vo.setThreeJdmlr(mlr);
                        }
                        if (jlr != null) {
                            vo.setThreeJdjlr(jlr);
                        }
                        break;
                    case "4":
                        if (yycb != null) {
                            vo.setFourJdyycb(yycb);
                        }
                        if (yysr != null) {
                            vo.setFourJdyysr(yysr);
                        }
                        if (mlr != null) {
                            vo.setFourJdmlr(mlr);
                        }
                        if (jlr != null) {
                            vo.setFourJdjlr(jlr);
                        }
                        break;
                }
            }
            BgHzNdCwbb cwbb = bgHzNdCwbbService.getByDjxhAndTjnd(djxh, tjnd);
            if (cwbb != null) {
                if (cwbb.getYycb() != null) {
                    vo.setYycb(cwbb.getYycb().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                }else {
                    vo.setYycb(yycbCount);
                }
                if (cwbb.getYysr() != null) {
                    vo.setYysr(cwbb.getYysr().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                }else {
                    vo.setYysr(yysrCount);
                }
                if (cwbb.getJlr()!=null) {
                    vo.setJlr(cwbb.getJlr().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                }else {
                    vo.setJlr(jlrCount);
                }
                vo.setMlr(mlrCount);
                vo.setMll(cwbb.getMll());
                vo.setJll(cwbb.getYyjll());
            } else {
                vo.setYycb(yycbCount);
                vo.setYysr(yysrCount);
                vo.setJlr(jlrCount);
                vo.setMlr(mlrCount);
            }
            arrayList.add(vo);
        }
        return arrayList;
    }

    @Override
    public List<QyLrFxVo> qyLrFx(BgNsrjbxxReq req) {
        String djxh = req.getDjxh();
        List<String> tjnds = DateUtils.getInstance().nearlyThreeYears(req.getKssj(), req.getJssj());
        List<QyLrFxVo> arrayList = new ArrayList<>();
        return null;
    }
}
